package com.HouseManage.view;

import com.HouseManage.utils.UserUtil;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MainView {
    public void init() {

        JFrame f = new JFrame("LoL");
        f.setSize(400, 150);
        f.setLocation(200,200);
        JPanel top = new JPanel();
        top.setLayout(new FlowLayout());
        JPanel pNorth = new JPanel();
        JPanel pCenter = new JPanel();

        JLabel title = new JLabel("房屋管理系统");

        top.add(title);

        JLabel l1 = new JLabel("账号");
        JTextField tf1= new JTextField();
        tf1.setPreferredSize(new Dimension(80,30));

        JLabel l2 = new JLabel("密码");
        JPasswordField tf2 = new JPasswordField();
        tf2.setPreferredSize(new Dimension(80,30));

        pNorth.add(l1);
        pNorth.add(tf1);
        pNorth.add(l2);
        pNorth.add(tf2);

        JButton b = new JButton("登录");
        JButton reg = new JButton("注册");
        JButton zhuxiao = new JButton("注销");

        pCenter.add(b);
        pCenter.add(reg);
        pCenter.add(zhuxiao);


        b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String name = tf1.getText();
                String password = new String(tf2.getPassword());
                if(0==name.length()){
                    JOptionPane.showMessageDialog(f, "账号不能为空");
                    tf1.grabFocus();
                    return;
                }
                if(0==password.length()){
                    JOptionPane.showMessageDialog(f, "密码不能为空");
                    tf2.grabFocus();
                    return;
                }
                if(check(name, password)) {
                    JOptionPane.showMessageDialog(f, "登陆成功");
                    f.setVisible(false);
                    new View();

                } else
                    JOptionPane.showMessageDialog(f, "密码错误");

            }


        });


        reg.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String name = tf1.getText();
                String password = new String(tf2.getPassword());
                if(0==name.length()){
                    JOptionPane.showMessageDialog(f, "账号不能为空");
                    tf1.grabFocus();
                    return;
                }
                if(0==password.length()){
                    JOptionPane.showMessageDialog(f, "密码不能为空");
                    tf2.grabFocus();
                    return;
                }
                if(check(name, password))
                    JOptionPane.showMessageDialog(f, "已存在该账号");
                else {
                    new UserUtil().addMessage(name, password);
                    JOptionPane.showMessageDialog(f, "注册成功");
                }
            }
        });

        zhuxiao.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String name = tf1.getText();
                String password = new String(tf2.getPassword());
                if(0==name.length()){
                    JOptionPane.showMessageDialog(f, "账号不能为空");
                    tf1.grabFocus();
                    return;
                }
                if(0==password.length()){
                    JOptionPane.showMessageDialog(f, "密码不能为空");
                    tf2.grabFocus();
                    return;
                }
                if(check(name, password)) {
                    new UserUtil().delMessage(name,password);
                    JOptionPane.showMessageDialog(f, "注销成功");
                }
                else {
                    JOptionPane.showMessageDialog(f, "账号输入错误");
                }
            }
        });



        f.setLayout(new BorderLayout());
        f.add(top,BorderLayout.NORTH);
        f.add(pNorth,BorderLayout.CENTER);
        f.add(pCenter,BorderLayout.SOUTH);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setVisible(true);

    }
    //数据库查询

    public boolean check(String name,String password){
        System.out.println(new UserUtil().checkState(name,password));
        return new UserUtil().checkState(name,password);
    }
}